/*
 * Copyright 2017 Zhongan.com All right reserved. This software is the
 * confidential and proprietary information of Zhongan.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Zhongan.com.
 */
package com.zhongan.gateway.web.config;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import com.netflix.zuul.FilterFileManager;
import com.netflix.zuul.FilterLoader;
import com.netflix.zuul.groovy.GroovyCompiler;
import com.netflix.zuul.groovy.GroovyFileFilter;

/**
 * @author
 */

public class GroovyServletContextListener implements ServletContextListener {
    private static final Logger logger = LoggerFactory.getLogger(GroovyServletContextListener.class);
    @Value("${gw.groovy.fileRoot}")
    private String              fileRoot;

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        logger.info("stopping server");
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {

        FilterLoader.getInstance().setCompiler(new GroovyCompiler());

        try {
            FilterFileManager.setFilenameFilter(new GroovyFileFilter());
            FilterFileManager.init(5, fileRoot + "pre");
            logger.info("load groovy...file: " + fileRoot);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

}
